package exammanager;

import gui.GUIMain;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import stuff.FixedSizeDocument;

public class GUIExamAdd extends JDialog{

	private static final long serialVersionUID = 1L;
	private JTextField examTitle;
	private JTextField examTime;
	
	private JButton btOK;
	private JButton btCancel;
	
	public GUIExamAdd(JFrame parent) {
		super(parent, "Thêm Đề thi", true);
		setLayout(new BorderLayout());
		JPanel titlePanel = new JPanel();
		titlePanel.setLayout(new BorderLayout(0,0));
		titlePanel.setBorder(new EmptyBorder(0, 10, 0, 10));
		JLabel titleLabel = new JLabel("Tên Đề thi");
		examTitle = new JTextField();
		examTitle.setDocument(new FixedSizeDocument(1999));
		titlePanel.add(titleLabel,BorderLayout.NORTH);
		titlePanel.add(examTitle,BorderLayout.CENTER);
		
		JPanel timePanel = new JPanel();
		timePanel.setBorder(new EmptyBorder(10, 10, 10, 10));
		JLabel timeLabel = new JLabel("Thời gian làm bài:  ");
		JLabel lbMinute = new JLabel(" phút.");
		timePanel.setLayout(new BorderLayout(0,0));
		
		NumberFormat f = NumberFormat.getNumberInstance(); 
		f.setMaximumIntegerDigits(3);
		examTime = new JFormattedTextField(f);
		timePanel.add(timeLabel,BorderLayout.WEST);
		timePanel.add(examTime,BorderLayout.CENTER);
		timePanel.add(lbMinute, BorderLayout.EAST);
		
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.setBorder(new EmptyBorder(0, 10, 0, 10));
		btOK = new JButton(new ButtonOKAction());
		btOK.setText("Tạo mới");
		btCancel = new JButton(new ButtonCancelAction());
		btCancel.setText("Thoát");
		buttonPanel.add(btOK);
		buttonPanel.add(btCancel);
		
		add(titlePanel,BorderLayout.CENTER);
		add(timePanel,BorderLayout.NORTH);
		add(buttonPanel,BorderLayout.SOUTH);
	}
	
	public boolean addExam(){
		if(examTitle.getText().trim().equals("")){
			JOptionPane.showMessageDialog(this, "Tên bài thi không được để trống!", 
					"Lỗi", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		if(examTime.getText().trim().equals("")){
			JOptionPane.showMessageDialog(this, "Thời gian làm bài không được để trống!", 
					"Lỗi", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		if(Integer.valueOf(examTime.getText()) == 0){
			JOptionPane.showMessageDialog(this, "Thời gian làm bài không thể là 0 phút!", 
					"Lỗi", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		if(updateDB()){
			return true;
		}else{
			return false;
		}
	}
	
	private boolean updateDB(){
		if(GUIMain.getDb().InsertExam(examTitle.getText().trim(), 60*Integer.valueOf(examTime.getText()))){
			JOptionPane.showMessageDialog(this, "Tạo mới Bài thi " + examTitle.getText().trim() + " thành công.", 
					"Thành công", JOptionPane.INFORMATION_MESSAGE);
			return true;
		}else{
			JOptionPane.showMessageDialog(this, "Tạo mới Bài thi " + examTitle.getText().trim() + " không thành công.", 
					"Lỗi", JOptionPane.INFORMATION_MESSAGE);
			return false;
		}
	}
	
	class ButtonOKAction extends AbstractAction{

		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(addExam()){
				dispose();
			}
		}
	}
	
	class ButtonCancelAction extends AbstractAction{

		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent arg0) {
			dispose();
		}
	}
}
